package com.itheima.Day0610.Demo02.Controller;

import com.itheima.Day0610.Demo02.Domain.Student;
import com.itheima.Day0610.Demo02.Service.StudentService;
import com.itheima.Day0610.Demo02.Service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    StudentService ss = new StudentService();

    public void start() {
        Scanner sc = new Scanner(System.in);
        wh:
        while (true) {
            System.out.println("--------------欢迎来到黑马学生管理系统-----------------");
            System.out.println("请输入你的选择： 1.添加学生  2.删除学生   3.修改学生  4.查看学生  5.退出");
            String S = sc.next();
            switch (S) {
                case "1":
                    System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    System.out.println("查看学生");
                    findStudent();

                    break;
                case "5":
                    System.out.println("退出");
                    break wh;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    private void updateStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入想要修改的学生学号");
        String id = sc.next();
        boolean flag = ss.checkId(id);
        if (flag == true){
            System.out.println("学号不存在");
            return;
        }
        // 如果没有结束方法，则继续录入
        System.out.println("请输入想要修改的学生姓名");
        String name = sc.next();
        System.out.println("请输入想要修改的学生年龄");
        String age = sc.next();
        System.out.println("请输入想要修改的学生生日");
        String birthday = sc.next();
        // 将用户录入的数据，封装到Student对象中。
        Student s = new Student();
        s.setId(id);
        s.setName(name);
        s.setAge(age);
        s.setBirthday(birthday);
        //将数据交给Service(业务员)
        ss.updateStudent(s);
        System.out.println("修改成功");

    }

    private void deleteStudent() {
        System.out.println("请输入要删除的学号");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        boolean flag = ss.deleteStudent(id);
        if (flag == false) {
            System.out.println("删除失败,您要删除的学号不存在");
        } else {
            System.out.println("删除成功");
        }
    }

    private void findStudent() {
        ArrayList<Student> list = ss.findStudent();
        if (list.size() == 0) {
            System.out.println("暂无学生信息,请先注册再来");
            return;
        }
        System.out.println("id---姓名---年龄---生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "---" + stu.getName() + "----" + stu.getAge() + "----" + stu.getBirthday());
        }
    }

    private void addStudent() {
        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入新的学生id");
            id = sc.next();
            boolean flag = ss.checkId(id);
            if (flag == false) {
                System.out.println("此学号已被占用，请重新输入");
                continue;
            }
            break;
        }
        System.out.println("请输入新的学生姓名");
        String name = sc.next();
        System.out.println("请输入新的学生年龄");
        String age = sc.next();
        System.out.println("请输入新的学生生日");
        String birthday = sc.next();
        Student S = new Student();
        S.setId(id);
        S.setName(name);
        S.setAge(age);
        S.setBirthday(birthday);
        ss.addStudent(S);
        System.out.println("添加成功");
    }
}
